#include "Tire.h"

GLuint Tire::baseList;
GLuint Tire::treadTex;
GLuint Tire::rimTex;
GLUquadric* Tire::quad;

void Tire::setup()
{
	treadTex = loadTex(TEXTURE_DIR + "tiretread.jpg");
	rimTex = loadTex(TEXTURE_DIR + "rim.jpg");
	quad = gluNewQuadric();
	gluQuadricTexture(quad, GL_TRUE);
	baseList = glGenLists(1);
	
	glNewList(baseList, GL_COMPILE);
	
		glPushMatrix();
		glPushAttrib(GL_ALL_ATTRIB_BITS);
			// Shiny rubber/metal
			GLfloat spec[] = {1.0, 1.0, 1.0, 1.0};
			glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
			glMaterialf(GL_FRONT, GL_SHININESS, 48);
	
			glEnable(GL_TEXTURE_2D);
			glBindTexture(GL_TEXTURE_2D, treadTex);
			glColor3f(1.0, 1.0, 1.0);
			gluCylinder(quad, 3.0, 3.0, 2.0, 20, 20);
		
			glPushMatrix();
				glTranslatef(0.0, 0.0, 2.0);
				glDisable(GL_TEXTURE_2D);
				glColor3f(0.125, 0.125, 0.125);	
				gluCylinder(quad, 3.0, 2.5, 0.4, 20, 20);
				
				glTranslatef(0.0, 0.0, 0.4);
				gluDisk(quad, 2.0, 2.5, 20, 20);
			
				glTranslatef(0.0, 0.0, -0.75);
				glColor3f(0.9, 0.9, 0.9);
				gluCylinder(quad, 1.6, 2.0, 0.75, 20, 20);
			
				glEnable(GL_TEXTURE_2D);
				glBindTexture(GL_TEXTURE_2D, rimTex);
				gluDisk(quad, 0.0, 1.6, 20, 20);
			glPopMatrix();
			
			glPushMatrix();
				glTranslatef(0.0, 0.0, -0.4);
				glDisable(GL_TEXTURE_2D);
				glColor3f(0.125, 0.125, 0.125);	
				gluCylinder(quad, 2.5, 3.0, 0.4, 20, 20);
				
				gluDisk(quad, 2.0, 2.5, 20, 20);
			
				glColor3f(0.9, 0.9, 0.9);
				gluCylinder(quad, 2.0, 1.6, 0.75, 20, 20);
			
				glTranslatef(0.0, 0.0, 0.75);
				glEnable(GL_TEXTURE_2D);
				glBindTexture(GL_TEXTURE_2D, rimTex);
				gluDisk(quad, 0.0, 1.6, 20, 20);
			glPopMatrix();
		glPopAttrib();
		glPopMatrix();
	
	glEndList();
}

void Tire::cleanup()
{
	glDeleteLists(baseList, 1);
	gluDeleteQuadric(quad);
	glDeleteTextures(1, &treadTex);
	glDeleteTextures(1, &rimTex);
}

Tire::Tire()
{
	rotation = 0;
}

Tire::~Tire()
{
}

void Tire::rotate(GLfloat angle)
{
	rotation += angle;
}

void Tire::draw() const
{
	glPushMatrix();
		glRotatef(rotation, 0.0, 0.0, 1.0);
		glCallList(baseList);
	glPopMatrix();
}

